<template>
  <div>
    <ul>
      <li v-for="item in goodsList" :key="item.id">
        <img :src="getgoodsImg(item.img)" width="100px" height="100px"/>
        <div class="footer">
           <p class="name">{{item.name}}</p>
        <div class="pricebox">
          <p class="price">
          ￥{{item.price}}
        </p>
        <el-button class="car" type="danger" size="small" @click="addcar(item.id)">加入购物车</el-button>
        </div>
        
        </div>
       
      </li>
    
    </ul>
  </div>
</template>
<script>
export default ({
  data(){
    return{
      goodsList:[],

    }
  },
  created(){
    this.getgoodsList();
  },
  methods:{
    getgoodsList(){
     this.$axios.get("/app/login/getgoodslist").then((res)=>{
      //  console.log(res);
      this.goodsList=res.data;
      // console.log(this.goodsList);
     })
    },
    getgoodsImg(xid){
      return require("../../server/upload/"+xid);
    },
    addcar(yid){
      this.$axios.post("/app/login/addcar",{
        idnum:yid,
        iduser:window.sessionStorage.getItem('token')
      }).then((res)=>{
        // console.log(res);
        if(res.data.state==201){
          this.$message.success("成功加入购物车");
          this.$router.push("/shopping")
        }
      })
    }
 }
})
</script>

<style scoped>
li{
  list-style: none;
  padding: 0;
  margin:10px;
 border:1px solid #e0e0e0;
  height: 200px;
  width: 200px;
  float: left;
}
li:hover{
  border: 1px solid crimson;
}
ul{
  height: auto;
  width: auto;
  
}
.pricebox{
  height: 40px;
  /* background-color: antiquewhite; */
  text-align: left;
  line-height: 10px;
  color: crimson;
  font-weight: 700;
  display: flex;
  /* justify-content: space-between; */
}
.price{
  flex: 2;
}
.car{
  flex: 1;

}
.name{
  height: 40px;
  /* background-color: aqua; */
  line-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.footer{
  /* background-color: rgb(72, 199, 22); */
  margin-top: -89px;
}
/* li img{
  border: 1px solid slateblue;
} */
</style>